package com.quectel.mobile.controller.mobileuser;


import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.mobileuser.dto.MobileUserMsgDto;
import com.quectel.core.module.mobileuser.service.MobileUserMsgService;
import com.quectel.mobile.controller.BaseController;
import com.quectel.mobile.util.SessionHolder;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/05/07 14:04
 */
@RestController
@RequestMapping("mobileUserMsg")
@Tag(name = "MobileUserMsgController", description = "用户消息")
public class MobileUserMsgController extends BaseController {

    @DubboReference
    private MobileUserMsgService mobileUserMsgService;

    @GetMapping("unreadStatus")
    @Operation(summary = "当前登录人是否有未读的消息,返回1代表有未读;返回0代表没有未读")
    public Response<Object> unreadStatus() {
        SessionHolder.checkMobileUser();
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        request.put(LambdaUtil.getFieldName(MobileUserMsgDto::getMobileUserId), SessionHolder.getLoginMobileUserSession().getMobileUser().getId());
        // 未读的
        request.put(LambdaUtil.getFieldName(MobileUserMsgDto::getReadStatus), SystemConstants.NO);

        int total = mobileUserMsgService.queryTotal(request.getParams());
        if (total > 0) {
            // 有未读的消息数
            return Response.ok().wrap(SystemConstants.YES);
        } else {
            // 没有查询到未读的消息数
            return Response.ok().wrap(SystemConstants.NO);
        }
    }

    /**
     * 列表
     */
    @GetMapping("list")
    @Operation(summary = "消息列表")
    public Response<Page<MobileUserMsgDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize,
            @Parameter(description = "字典值:MobileUserMsgType 1、物业缴费；2。。。") @RequestParam(required = false) Byte sourceType,
            @Parameter(description = "开始时间 2022-01-01 00:00:00") @RequestParam(required = false) String startTime,
            @Parameter(description = "结束时间 2022-01-01 23:59:59") @RequestParam(required = false) String endTime,
            @Parameter(description = "已读未读 1已读0未读") @RequestParam(required = false) Byte readStatus
    ) {

        SessionHolder.checkMobileUser();

        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        request.getParams().put(LambdaUtil.getFieldName(MobileUserMsgDto::getMobileUserId), String.valueOf(SessionHolder.getLoginMobileUserSession().getMobileUser().getId()));
        if (sourceType != null) {
            request.getParams().put(LambdaUtil.getFieldName(MobileUserMsgDto::getSourceType), String.valueOf(sourceType));
        }

        int total = mobileUserMsgService.queryTotal(request.getParams());

        List<MobileUserMsgDto> list = null;
        if (total > 0) {
            list = mobileUserMsgService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<MobileUserMsgDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<MobileUserMsgDto>>ok().wrap(page);
    }

    /**
     * 列表
     */
    @PostMapping("signRead")
    @Operation(summary = "标记已读")
    public Response<Object> signRead(@RequestBody Long id) {
        MobileUserMsgDto mobileUserMsgDto = mobileUserMsgService.selectById(id);
        if (!Objects.requireNonNull(SessionHolder.getLoginMobileUserSession()).getMobileUser().getId().equals(mobileUserMsgDto.getMobileUserId())) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        if (SystemConstants.YES.equals(mobileUserMsgDto.getReadStatus())) {
            return Response.ok();
        }
        mobileUserMsgDto.setReadStatus(SystemConstants.YES);
        mobileUserMsgDto.setReadTime(new Date());
        mobileUserMsgService.updateById(mobileUserMsgDto);

        return Response.ok();
    }

    /**
     * 列表
     */
    @PostMapping("signReadAll")
    @Operation(summary = "标记全部已读")
    public Response<Object> signReadAll() {
        Map<String, Object> params = new HashMap<>();
        params.put(LambdaUtil.getFieldName(MobileUserMsgDto::getMobileUserId), String.valueOf(SessionHolder.getLoginMobileUserSession().getMobileUser().getId()));
        params.put(LambdaUtil.getFieldName(MobileUserMsgDto::getReadStatus), SystemConstants.NO.toString());
        List<MobileUserMsgDto> mobileUserMsgDtos = mobileUserMsgService.queryList(params);
        if (mobileUserMsgDtos != null) {
            // todo 存在分布式事物问题 待修改
            for (MobileUserMsgDto mobileUserMsgDto : mobileUserMsgDtos) {
                mobileUserMsgDto.setReadStatus(SystemConstants.YES);
                mobileUserMsgDto.setReadTime(new Date());
                mobileUserMsgService.updateById(mobileUserMsgDto);
            }
        }
        return Response.ok();
    }
}
